package com.lovecanon.jzjl.web.controller;


import com.lovecanon.jzjl.common.BtPage;
import com.lovecanon.jzjl.common.RedisService;
import com.lovecanon.jzjl.common.ResultData;
import com.lovecanon.jzjl.domain.TbReceive;
import com.lovecanon.jzjl.mapper.TbReceiveMapper;
import com.lovecanon.jzjl.utils.ExportExcelUtil;
import com.lovecanon.jzjl.web.BaseController;
import com.lovecanon.jzjl.web.service.ReceiveService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("receivecontroller/")
public class ReceiveController extends BaseController {


    @Autowired
    private ReceiveService receiveService;

    @Autowired
    private RedisService redisServicel;

    @Autowired
    private TbReceiveMapper tbReceiveMapper;
    @RequestMapping("/index")
    public ModelAndView findAll(ModelAndView modelAndView) {
        redisServicel.set("Login","123456");
        modelAndView.setViewName("index");
        return modelAndView;
    }


    @RequestMapping("/getPage")
    @ResponseBody
    public BtPage getPage(HttpServletRequest request) {
        HashMap<String,Object> queryMap = new HashMap<>();
        String fileName = request.getParameter("fileName");
        int pageNumber =  Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("limit"));
        queryMap.put("pageNumber",pageNumber);
        queryMap.put("pageSize",pageSize);
        if (!StringUtils.isEmpty(fileName)){
            queryMap.put("fileName",fileName);
        }
        return receiveService.findAll(queryMap);
    }

    @RequestMapping("/SaveOrUpdate")
    @ResponseBody
    public ResultData save(TbReceive receive, HttpServletRequest request) {
        ResultData resultData = new ResultData();
        resultData.requestError("图包已经存在,请勿重复添加");
        TbReceive tbreceiveByFileName = receiveService.getTbreceiveByFileName(receive.getFileName());
        if (tbreceiveByFileName!=null){
            return resultData;
        }
        receive.setIsUse("Y");
         return  receiveService.saveOrUpdate(receive);
    }


    @RequestMapping("/updateStatus")
    @ResponseBody
    public ResultData updateStatus( HttpServletRequest request) {
        ResultData resultData = new ResultData();
        String is_send = request.getParameter("is_send");
        String id = request.getParameter("id");
        String is_finish = request.getParameter("is_finish");
        if (!"".equals(id)){
            TbReceive receive1 =  receiveService.findOne(Long.parseLong(id));
            if (!StringUtils.isEmpty(is_send)){
                if (receive1.getIsSend()==1){
                    resultData.requestError("状态已经完成，请勿重新点击");
                    return resultData;
                }
                receive1.setSendTime(new Date());
                receive1.setIsSend(1);
                receiveService.saveOrUpdate(receive1);
                resultData.requestSuccess();
            }else if (!StringUtils.isEmpty(is_finish)){
                if (receive1.getIsFinish()==1){
                    resultData.requestError("状态已经完成，请勿重新点击");
                    return resultData;
                }
                receive1.setFinishTime(new Date());
                receive1.setIsFinish(1);
                receiveService.saveOrUpdate(receive1);
                resultData.requestSuccess();
            }
        }else {
            resultData.requestError();
        }
        return resultData ;
    }


    @RequestMapping("/exportExl")
    public void exportExlByMyOrder(HttpServletRequest request,HttpServletResponse response){
        try {
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");

            List<TbReceive> all = tbReceiveMapper.findAll();
            List<Object[]> data = new ArrayList<>();
            Object[] objs = null;

            if (all!=null&&all.size()>0){
                for (int i = 0; i < all.size(); i++) {
                    objs = new Object[7];
                    objs[0] = all.get(i).getFileName();
                    objs[1] = all.get(i).getFileNumber();
                    objs[2] = all.get(i).getCreateTime();
                    objs[3] = all.get(i).getPrice();
                    objs[4] = all.get(i).getRemark();
                    objs[5] = all.get(i).getIsFinish();
                    objs[6] = all.get(i).getIsSend();
                    data.add(objs);
                }
            }
            //创建并获取工作簿对象
            Workbook wb;
            String fileName = "我的订单";
            String[] title = new String[7];
            title[0] = "文件名称";
            title[1] = "文件数量";
            title[2] = "创建时间";
            title[3] = "价格";
            title[4] = "备注";
            title[5] = "是否完成";
            title[6] = "是否发送";

           wb = new ExportExcelUtil().getWorkBook(title, data);
            exportExl(fileName, wb, response);
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    @RequestMapping("/delfile")
    @ResponseBody
    public ResultData delfile(HttpServletRequest request){
        ResultData resultData = new ResultData();
        String[] ids = request.getParameterValues("ids");
        try {
            receiveService.deleByIds(ids);
           return resultData.requestSuccess();
        }catch (Exception e){
            e.printStackTrace();
            return resultData.requestError();
        }
    }

    @RequestMapping("/findOne")
    @ResponseBody
    public  TbReceive findOne(HttpServletRequest request){
       return receiveService.findOne(Long.parseLong(request.getParameter("id")));
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public List<TbReceive> findAll(){
        return tbReceiveMapper.findAll();
    }



}
